package userspecmodel

import (
	"strconv"
	"time"
	"web_server/model"
)

// type UserSpecElem struct {
// 	Id     int64
// 	Userid int64
// 	Specid int64
// 	Timedate   string
// }

type UserSpecModel struct {
	Id       int64  `json:"id"`
	Namecode string `json:"namecode"`
	Username string `json:"username"`
	Specname string `json:"specname"`
	Timedate string `json:"timedate"`
}

func TimedateValid(timedate string, ok bool) *string {
	if !ok {
		return nil
	}
	rtime, err := time.ParseInLocation("2006-01-02", timedate, time.Local)
	if err == nil {
		newstr := new(string)
		*newstr = rtime.Format("2006-01-02")
		return newstr
	}
	return nil
}

func GetUserSpecModel(id int64) *UserSpecModel {
	var user *UserSpecModel = new(UserSpecModel)
	if err := model.DBSlaveRand().Get(user, "SELECT t1.id, t2.namecode, t2.username, t3.name AS specname, t1.timedate FROM t_fruit_user_species t1 LEFT JOIN t_base_user t2 ON t1.userid = t2.id  LEFT JOIN t_fruit_species t3 ON t1.specid = t3.id WHERE t1.id = ?", id); err != nil {
		// panic(err)
		return nil
	}
	return user
}

func GetUserSpecList(pagenum int64) ([]UserSpecModel, int64) {
	var usmlist []UserSpecModel
	var specNum int64
	if err := model.DBSlaveRand().Get(&specNum, "SELECT count(*) FROM t_fruit_user_species"); err != nil {
		panic(err)
	}
	pagemax := (specNum + 19) / 20
	if pagenum > pagemax {
		pagenum = pagemax
	}
	if err := model.DBSlaveRand().Select(&usmlist, "SELECT t1.id, t2.namecode, t2.username, t3.name AS specname, t1.timedate FROM t_fruit_user_species t1 LEFT JOIN t_base_user t2 ON t1.userid = t2.id LEFT JOIN t_fruit_species t3 ON t1.specid = t3.id LIMIT 20 OFFSET ?", (pagenum-1)*20); err != nil {
		panic(err)
	}
	for i := range usmlist {
		usmlist[i].Timedate = usmlist[i].Timedate[0:10]
	}
	return usmlist, pagemax
}

func GetUserSpecListByCode(namecode string, pagenum int64) ([]UserSpecModel, int64) {
	var usmlist []UserSpecModel
	var specNum int64
	if err := model.DBSlaveRand().Get(&specNum, "SELECT count(*) FROM t_fruit_user_species t1 LEFT JOIN t_base_user t2 ON t1.userid = t2.id WHERE t2.namecode = ?", namecode); err != nil {
		panic(err)
	}
	pagemax := (specNum + 19) / 20
	if pagenum > pagemax {
		pagenum = pagemax
	}
	if err := model.DBSlaveRand().Select(&usmlist, "SELECT t1.id, t2.namecode, t2.username, t3.name AS specname, t1.timedate FROM t_fruit_user_species t1 LEFT JOIN t_base_user t2 ON t1.userid = t2.id LEFT JOIN t_fruit_species t3 ON t1.specid = t3.id WHERE t2.namecode = ? LIMIT 20 OFFSET ?", namecode, (pagenum-1)*20); err != nil {
		panic(err)
	}
	for i := range usmlist {
		usmlist[i].Timedate = usmlist[i].Timedate[0:10]
	}
	return usmlist, pagemax
}

func GetUserSpecListByTime(timedate string, pagenum int64) ([]UserSpecModel, int64) {
	var usmlist []UserSpecModel
	var specNum int64
	if err := model.DBSlaveRand().Get(&specNum, "SELECT count(*) FROM t_fruit_user_species WHERE timedate = ?", timedate); err != nil {
		panic(err)
	}
	pagemax := (specNum + 19) / 20
	if pagenum > pagemax {
		pagenum = pagemax
	}
	if err := model.DBSlaveRand().Select(&usmlist, "SELECT t1.id, t2.namecode, t2.username, t3.name AS specname, t1.timedate FROM t_fruit_user_species t1 LEFT JOIN t_base_user t2 ON t1.userid = t2.id LEFT JOIN t_fruit_species t3 ON t1.specid = t3.id WHERE t1.timedate = ? LIMIT 20 OFFSET ?", timedate, (pagenum-1)*20); err != nil {
		panic(err)
	}
	for i := range usmlist {
		usmlist[i].Timedate = usmlist[i].Timedate[0:10]
	}
	return usmlist, pagemax
}

func SetUserSpecAdd(usm UserSpecModel) error {
	_, err := model.DBMaster().Exec("INSERT INTO t_fruit_user_species(userid, specid, timedate) SELECT t2.id, t3.id, ? FROM t_base_user t2, t_fruit_species t3 WHERE t2.namecode = ? AND t3.name = ?", usm.Timedate, usm.Namecode, usm.Specname)
	return err
}

func SetUserSpecEdit(usm UserSpecModel) error {
	_, err := model.DBMaster().Exec("UPDATE t_fruit_user_species SET userid = (SELECT id FROM t_base_user WHERE namecode = ?), specid = (SELECT id FROM t_fruit_species WHERE name = ?), timedate = ? WHERE id = ?", usm.Namecode, usm.Specname, usm.Timedate, usm.Id)
	return err
}

func SetUserSpecDel(ids []int) error {
	idsStr := strconv.Itoa(ids[0])
	for i := 1; i < len(ids); i++ {
		idsStr += ","
		idsStr += strconv.Itoa(ids[i])
	}
	_, err := model.DBMaster().Exec("DELETE FROM t_fruit_user_species WHERE id IN(" + idsStr + ")")
	return err
}
